﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.ComponentModel;

namespace SqlCeManager.DbUpdate
{
    public class ByteArrayAsStringEdit : MemoExEdit
    {
        protected override void CreateRepositoryItem()
        {
            fProperties = new RepositoryItemByteArrayAsStringEdit(this);
        }

        [Category("Properties")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemByteArrayAsStringEdit Properties
        {
            get { return fProperties as RepositoryItemByteArrayAsStringEdit; }
        }
        
    }

    public class RepositoryItemByteArrayAsStringEdit : RepositoryItemMemoExEdit
    {
        public RepositoryItemByteArrayAsStringEdit()
        {
            Initialize();
        }
        public RepositoryItemByteArrayAsStringEdit(ByteArrayAsStringEdit edit)
        {
            SetOwnerEdit(edit);
            Initialize();
        }

        private void Initialize()
        {
            this.CustomDisplayText += new DevExpress.XtraEditors.Controls.CustomDisplayTextEventHandler(RepositoryItemByteArrayAsStringEdit_CustomDisplayText);
            this.ParseEditValue += new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(RepositoryItemByteArrayAsStringEdit_ParseEditValue);
            this.FormatEditValue += new DevExpress.XtraEditors.Controls.ConvertEditValueEventHandler(RepositoryItemByteArrayAsStringEdit_FormatEditValue);
        }

        void RepositoryItemByteArrayAsStringEdit_FormatEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
        {
            if (e.Value is string)
            {
                e.Value = e.Value.AsString().AsByteArray();
                e.Handled = true;
            }
        }

        void RepositoryItemByteArrayAsStringEdit_ParseEditValue(object sender, DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs e)
        {
            if (e.Value is byte[])
            {
                e.Value = e.Value.AsString();
                e.Handled = true;
            }
        }
        void RepositoryItemByteArrayAsStringEdit_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (e.Value is byte[])
            {
                e.DisplayText = e.Value.AsString();
            }
        }
        protected override DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs DoFormatEditValue(object val)
        {
            return base.DoFormatEditValue(val);
        }
        protected override DevExpress.XtraEditors.Controls.ConvertEditValueEventArgs DoParseEditValue(object val)
        {
            return base.DoParseEditValue(val);
        }
        public override string GetDisplayText(DevExpress.Utils.FormatInfo format, object editValue)
        {
            if (editValue != null && editValue is byte[])
            {
                return editValue.AsString();
            }
            else return base.GetDisplayText(format, editValue);
        }

        private string GetString(object value)
        {
            return "";
        }
        private byte[] GetBytes(string value)
        {
            return new byte[0];
        }
    }
}
